#include "DirectInput.h"

DirectInput::DirectInput(void)
{

}

DirectInput::DirectInput(DWORD	keyboardCoopFlags, DWORD mouseCoopFlags)
{

}
DirectInput::~DirectInput(void)
{

}

void DirectInput::initDInput(HINSTANCE hInstance, HWND hWnd)
{
 // create the DirectInput interface
    DirectInput8Create(hInstance,    // the handle to the application
                       DIRECTINPUT_VERSION,    // the compatible version
                       IID_IDirectInput8,    // the DirectInput interface version
                       (void**)&din,    // the pointer to the interface
                       NULL);    // COM stuff, so we'll set it to NULL

    // create the keyboard device
    din->CreateDevice(GUID_SysKeyboard,    // the default keyboard ID being used
                      &dinkeyboard,    // the pointer to the device interface
                      NULL);    // COM stuff, so we'll set it to NULL

	din->CreateDevice(GUID_SysMouse, &dinmouse, NULL);

    // set the data format to keyboard format
    dinkeyboard->SetDataFormat(&c_dfDIKeyboard);

	dinmouse->SetDataFormat(&c_dfDIMouse2);

    // set the control you will have over the keyboard
    dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);

	dinmouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
}

void DirectInput::detect_input(void)
{
    // get access if we don't have it already
    dinkeyboard->Acquire();
	dinmouse->Acquire();

    // get the input data
    dinkeyboard->GetDeviceState(256, (LPVOID)buffer);
	dinmouse->GetDeviceState(sizeof(DIMOUSESTATE2), &m_mouseState);
}

void DirectInput::cleanDInput(void)
{
    dinkeyboard->Unacquire();// make sure the keyboard is unacquired
    dinmouse->Unacquire();
	din->Release();    // close DirectInput before exiting
}

bool DirectInput::keyDown(unsigned char key)
{
	if(buffer[key] & 0x80)
	{
		return true;
	}
	return false;
}

bool DirectInput::mouseButtonDown(int button)
{
	if(m_mouseState.rgbButtons[button] & 0x80)
	{
		return true;
	}
	return false;
}

float DirectInput::mouseX()
{
	float X = (float)(m_mouseState.lX);
	return X;
}
float DirectInput::mouseY()
{
	float Y = (float)(m_mouseState.lY);
	return Y;
}